# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
 -------------------------------------------------
    File Name:     scene_to_case.py
    Description:   
 -------------------------------------------------
 """
from rest_framework.decorators import action
from rest_framework.exceptions import MethodNotAllowed
from rest_framework.permissions import IsAuthenticated

from apps.scenes.models import SceneToCase, TestScene
from utils.json_response import DetailResponse
from utils.serializers import CustomModelSerializer
from utils.viewset import CustomModelViewSet


class SceneToCaseSerializer(CustomModelSerializer):
    """
    执行步骤-序列化器
    """

    class Meta:
        model = SceneToCase
        fields = '__all__'


class SceneToCaseListSerializer(CustomModelSerializer):
    """
    执行步骤-列表序列化器
    """
    from apps.api_tests.views.interface_cases import InterFaceCaseListSerializer
    # from apps.scenes.views.scene import TestSceneSerializer
    icase = InterFaceCaseListSerializer()

    # scene = TestSceneSerializer()

    class Meta:
        model = SceneToCase
        fields = '__all__'


class SceneToCaseView(CustomModelViewSet):
    """测试业务流中的用例执行顺序的接口"""
    queryset = SceneToCase.objects.all()
    serializer_class = SceneToCaseSerializer
    list_serializer_class = SceneToCaseListSerializer
    # permission_classes = [permissions.IsAuthenticated]
    filterset_fields = ['scene']

    def update(self, request, *args, **kwargs):
        return MethodNotAllowed("不允许使用此方法")

    def retrieve(self, request, *args, **kwargs):
        return MethodNotAllowed("不允许使用此方法")

    @action(methods=['PATCH'], detail=False, permission_classes=[IsAuthenticated])
    def order(self, request):
        """
        修改测试场景下的测试步骤的顺序

        request.data:
        {
            "id": 1,
            "sort": 2
        }
        """
        datas = request.data
        for item in datas:
            obj = SceneToCase.objects.get(id=item['id'])
            obj.sort = item['sort']
            obj.save()
        return DetailResponse(msg="修改成功")
